package ysq.xyg.sys.controller;

import cn.hutool.core.util.StrUtil;
import cn.org.rapid_framework.generator.GeneratorFacade;
import cn.org.rapid_framework.generator.GeneratorProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.sys.model.vo.SysTableVo;
import ysq.xyg.sys.service.SysGeneratorService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * 系统部分-代码生成controller层
 *
 * @author shengQiang yu
 * @date 2018-03-18 15:42
 */
@RestController
public class SysGeneratorController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(SysGeneratorController.class);
    @Resource
    private SysGeneratorService sysGeneratorService;


    /**
     * 分页查询
     *
     * @param sysTableVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    @RequestMapping(value = "/sysGenerator/page",method = RequestMethod.POST)
    public ReturnEntity page(SysTableVo sysTableVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = sysGeneratorService.page(sysTableVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 跳转list页面生成页面
     *
     * @param tableName 表名
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    @RequestMapping(value = "/sysGenerator/getCreateListPage",method = RequestMethod.GET)
    public ModelAndView getCreateListPage(String tableName){
        ModelAndView view = new ModelAndView("/sys/sysGeneratorCreateListPage");
        view.addObject("sysUseColumnList", sysGeneratorService.listUseColumnsByTableName(tableName,"remove_tag,create_time,create_user,update_time,update_user"));
        view.addObject("sysColumnList", sysGeneratorService.listUseColumnsByTableName(tableName,"remove_tag"));
        view.addObject("path", BaseUtil.toTfmm(tableName));
        view.addObject("sysTableVo", sysGeneratorService.selectByTableName(tableName));
        return view;
    }
    /**
     * 跳转编辑页面生成页面
     *
     * @param tableName 表名
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    @RequestMapping(value = "/sysGenerator/getCreateEditPage",method = RequestMethod.GET)
    public ModelAndView getCreateEditPage(String tableName){
        ModelAndView view = new ModelAndView("/sys/sysGeneratorCreateEditPage");
        view.addObject("sysUseColumnList", sysGeneratorService.listUseColumnsByTableName(tableName,"remove_tag,create_time,create_user,update_time,update_user"));
        view.addObject("path", BaseUtil.toTfmm(tableName));
        view.addObject("sysTableVo", sysGeneratorService.selectByTableName(tableName));
        return view;
    }


    /**
     * 跳转代码生成连接
     *
     * @param tableName 表名
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    @RequestMapping(value = "/sysGenerator/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String tableName){
        ModelAndView view = new ModelAndView("/sys/sysGeneratorUpdateOrSave");
        SysTableVo sysTableVo=sysGeneratorService.selectByTableName(tableName);
        if(sysTableVo!=null&& StrUtil.isNotEmpty(sysTableVo.getTableComment())){
            String[] args=sysTableVo.getTableComment().split("-");
            sysTableVo.setModuleLogName(args[0]);
            if(args.length>1){
                sysTableVo.setClassLogName(args[1]);
            }
        }
        view.addObject("sysTableVo", sysTableVo);
        return view;
    }

    @RequestMapping(value = "/sysGenerator/createCode", method = RequestMethod.POST)
    public ReturnEntity export(HttpServletRequest request, SysTableVo sysTableVo) throws Exception {
        String projectPath=request.getSession().getServletContext().getRealPath("/");
        projectPath=projectPath.substring(0,projectPath.indexOf("target"));
        sysTableVo.setProjectPath(projectPath);
        setProperties(sysTableVo);
        deleteFiles(sysTableVo);
        try {
            GeneratorFacade generatorFacade = new GeneratorFacade();
            generatorFacade.getGenerator().setTemplateRootDir(sysTableVo.getProjectPath() + "/src/main/resources/template/general/");
            generatorFacade.generateByTable(sysTableVo.getTableName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ReturnEntity();
    }
    private void deleteFiles(SysTableVo sysTableVo) {
        String path = sysTableVo.getProjectPath() + "/src/main/java/ysq/xyg/" + sysTableVo.getModuleName() + "/";
        File controller = new File((path + "controller/" + sysTableVo.getClassName() + "Controller.java"));
        if (controller.exists()) {
            controller.delete();
        }
        File service = new File((path + "service/" + sysTableVo.getClassName() + "Service.java"));
        if (service.exists()) {
            service.delete();
        }
        File serviceImpl = new File((path + "service/impl/" + sysTableVo.getClassName() + "ServiceImpl.java"));
        if (serviceImpl.exists()) {
            serviceImpl.delete();
        }
        File dao = new File((path + "dao/" + sysTableVo.getClassName() + "Dao.java"));
        if (dao.exists()) {
            dao.delete();
        }
        File po = new File((path + "model/po/" + sysTableVo.getClassName() + ".java"));
        if (po.exists()) {
            po.delete();
        }
        File vo = new File((path + "model/vo/" + sysTableVo.getClassName() + "Vo.java"));
        if (vo.exists()) {
            vo.delete();
        }
    }
    public static void setProperties(SysTableVo sysTableVo) throws Exception {
        GeneratorProperties.setProperty("outRoot", sysTableVo.getProjectPath() + "/src/main/java/ysq/xyg/" + sysTableVo.getModuleName());
        GeneratorProperties.setProperty("tableComment", sysTableVo.getModuleLogName()+"-"+sysTableVo.getClassLogName());
        GeneratorProperties.setProperty("moduleLogName", sysTableVo.getModuleLogName());
        GeneratorProperties.setProperty("classLogName", sysTableVo.getClassLogName());
        GeneratorProperties.setProperty("module", sysTableVo.getModuleName());
        GeneratorProperties.setProperty("entity", sysTableVo.getClassName());
        GeneratorProperties.setProperty("author", sysTableVo.getAuthor());
    }
}